﻿using System;
using System.Collections.Generic;

namespace SBPweb.MediaCenter.Common.Smartparts
{
    public abstract class SmartpartListBase<T> : List<T>, ISmartpartList where T : ISmartpart
    {
        
        #region Constructor logic

        protected SmartpartListBase()
        {

        }

        #endregion

        #region ISmartpartList Members

        public ISmartpart[] Items
        {
            get
            {
                return Array.ConvertAll<T, ISmartpart>(base.ToArray(), new Converter<T, ISmartpart>(ConvertTToISmartpart));
            }
        }

        public event EventHandler<SmartpartEventArgs> ItemAdded;

        public event EventHandler<SmartpartEventArgs> ItemRemoved;

        #endregion

        #region Helper methods

        public static ISmartpart ConvertTToISmartpart(T t)
        {
            return (ISmartpart)t;
        }

        #endregion


        #region IList<ISmartpart> Members

        public int IndexOf(ISmartpart item)
        {
            return base.IndexOf((T)item);
        }

        public void Insert(int index, ISmartpart item)
        {
            base.Insert(index, (T)item);
            if (ItemAdded != null)
            {
                ItemAdded.Invoke(this, new SmartpartEventArgs(item));
            }
        }

        public new ISmartpart this[int index]
        {
            get
            {
                return (ISmartpart)base[index];
            }
            set
            {
                base[index] = (T)value;
            }
        }

        #endregion

        #region ICollection<ISmartpart> Members

        public void Add(ISmartpart item)
        {
            base.Add((T)item);
            if (ItemAdded != null)
            {
                ItemAdded.Invoke(this, new SmartpartEventArgs(item));
            }
        }

        public bool Contains(ISmartpart item)
        {
            return base.Contains((T)item);
        }

        public void CopyTo(ISmartpart[] array, int arrayIndex)
        {
            throw new NotImplementedException();
        }

        public bool IsReadOnly
        {
            get
            {
                return false;
            }
        }

        public bool Remove(ISmartpart item)
        {
            bool ret = base.Remove((T)item);
            if (ItemRemoved != null)
            {
                ItemRemoved.Invoke(this, new SmartpartEventArgs(item));
            }
            return ret;
        }

        #endregion

        #region IEnumerable<ISmartpart> Members

        public new IEnumerator<ISmartpart> GetEnumerator()
        {
            throw new NotImplementedException();
        }

        #endregion

    }

}
